var express = require('express');
var router = express.Router();
var mysql = require('mysql');
const { mysqlCreate } = require('./utils');

//连接数据库
let conn = mysqlCreate();

//获取所有文章
router.get('/', (req, res) => {
  conn = mysql.createConnection(conn.config);
  const type = req.query;
  if(JSON.stringify(type) === "{}"){
    var sql = `SELECT * FROM article`;
  }else if(type){
    var sql = `SELECT * FROM article where user = ${type.user}`;
  }else{
    res.json({code:-1, message:"参数获取错误"});
    return;
  }
  //连接数据库
  conn.connect();
  conn.query(sql, function (err, result) {
    if(err){
      console.log('[SELECT ERROR] - ',err.message);
      res.json({ code: -1})
    }else{
      res.json({ code: 10000, message: "OK", data: result})
    } 
  });
  conn.end();
})

module.exports = router;
